package com.fuhe.chen.vendingmachine.service.impl;

import com.alibaba.fastjson.JSON;
import com.fuhe.chen.vendingmachine.common.mqtt.ServerMQTT;
import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.dto.MQTTMessage;
import com.fuhe.chen.vendingmachine.pojo.BookOrder;
import com.fuhe.chen.vendingmachine.service.IMQTTService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MQTTServiceImpl implements IMQTTService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MQTTServiceImpl.class);

    public static final String ENCODE = "bp1gicbxlla9x1928xdw";

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ServerMQTT serverMQTT;

    @Override
    public void sendMessageForPurchase(String out_trade_no) {
        Map<Object,Object> redisCommodity= redisUtils.hmget(out_trade_no);
        List<MQTTMessage> messages = new ArrayList<>();
        redisCommodity.forEach((id,commodityStr)->{
            MQTTMessage message = new MQTTMessage();
            String [] str = ((String)commodityStr).split("_");
            message.setContainerId(str[5]);
            message.setInstruction("open");
            message.setKey(ENCODE);
            message.setStatus(UUID.randomUUID().toString().replace("-", ""));
            message.setTimestamp(String.valueOf((new Date()).getTime()));
            message.setMachineId(str[4]);
            messages.add(message);
        });
        try{
            LOGGER.info(JSON.toJSONString(messages));
            serverMQTT.sendMessage("forTest1", JSON.toJSONString(messages));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void sendMessageForPick(BookOrder order) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        List<MQTTMessage> messages = new ArrayList<>();
        MQTTMessage message = new MQTTMessage();
        message.setContainerId(order.getContainer_number());
        message.setInstruction("pick");
        message.setKey(ENCODE);
        message.setStatus(uuid);
        message.setTimestamp(String.valueOf((new Date()).getTime()));
        message.setMachineId(String.valueOf(order.getMachine_id()));
        messages.add(message);

        try{
            LOGGER.info(JSON.toJSONString(messages));
            serverMQTT.sendMessage("forTest1", JSON.toJSONString(messages));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void sendMessageForNoticeToPick(String userId) {
        List<MQTTMessage> messages = new ArrayList<>();
        MQTTMessage message = new MQTTMessage();
        message.setContainerId(userId);
        message.setInstruction("deliver_success");
        message.setTimestamp(String.valueOf((new Date()).getTime()));
        messages.add(message);

        try{
            LOGGER.info(JSON.toJSONString(messages));
            serverMQTT.sendMessage("forTest1", JSON.toJSONString(messages));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
